---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}

```{r setup, include=FALSE}
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
p_load(tidyverse, arrow, yaml, here, argparse, glue, knitr, kableExtra, scales)


# Working directory and command line argument setup
current_script = "generate_tables_and_figures/gen_pathway_reweighted_table/src/print_ao_table.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))

task_dir = here(dirname(dirname(file.path(current_script))))

knitr::opts_knit$set(root.dir = task_dir)


cl_args <- parse_make_args(c(
  "RMD_FILE",
  "REG_OUTPUT",
  "OUTCOME_LABELS_FILE",
  "SUBGROUP_OUTPUT",
  "OUT_FILE"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()

timestamp <- get_timestamp()
  
```
\newpage

\blandscape
##  Andrews and Oster Table


  
```{r}
reg_output <- read_csv(cl_args$REG_OUTPUT) %>%
  select(outcome, n, type, beta_itt, ao_itt)

ylabels <- read_csv(cl_args$OUTCOME_LABELS_FILE) %>% 
  select(outcome = value, label, primary_component_order)
  
# pull in outreach ITT
cr_subgroup_itt <- read_csv(cl_args$SUBGROUP_OUTPUT) %>%
  filter(subgroup == "pathway__cr") %>%
  select(outcome, n, cr = beta_itt)

# pull Ns for each common support type
ns <- reg_output %>%
  distinct(n, type) %>%
  bind_rows(cr_subgroup_itt %>% distinct(n) %>% mutate(type = "cr")) %>%
  pivot_wider(names_from = type, values_from =  n)%>%
  mutate(label = "N") %>%
  mutate_all(as.character)

#reshape table
ols_betas <- reg_output %>%
  select(outcome, type, beta_itt) %>%
  pivot_wider(names_from = type, values_from = beta_itt)

ao_betas <- reg_output %>% 
  select(outcome, type, ao_itt) %>%
  filter(!is.na(ao_itt)) %>%
  pivot_wider(names_from = type, values_from = ao_itt) %>%
  rename(re_exclusion_ao = re_exclusion, ul_exclusion_ao = ul_exclusion)

ao_ns <- reg_output %>% 
  filter(!is.na(ao_itt)) %>%
  distinct(type, n) %>%
  pivot_wider(names_from = type, values_from =  n) %>%
  rename(re_exclusion_ao = re_exclusion, ul_exclusion_ao = ul_exclusion) %>%
  mutate_all(as.character)

ns <- ns %>% cbind(ao_ns)

full_table <- ols_betas %>%
  left_join(cr_subgroup_itt %>% select(outcome, cr)) %>%
  left_join(ao_betas) %>%
  left_join(ylabels)  %>%
  mutate(across(where(is.numeric), ~round(.x,4) %>% format(., nsmall = 4) %>% as.character())) %>%
  arrange(primary_component_order) %>%
  bind_rows(ns,.) %>%
  select(label, cr, cr_ul_exclusion, ul_exclusion, ul_exclusion_ao, cr_re_exclusion, re_exclusion, re_exclusion_ao) %>%
  mutate(label = case_when(label == "Primary Index of Serious Violence" ~ "", 
                           T ~ label))


table <- full_table %>%
  kable(booktabs=T, longtable=T,
          col.names = c("", "ITT", rep(c("ITT", "ITT", "Implied ITT with Similar Xs"),2)),
          align=c("l", rep("c",7))) %>% 
  kable_styling(font_size = 11) %>%
  add_header_above(c(" "= 1, "Outreach" = 1,  "Outreach" = 1, "Algorithm" = 2, "Outreach" = 1,"Re-entry" = 2))  %>%
  add_header_above(c(" "= 2, "Algorithm: Common Support" = 3, "Re-entry: Common Support" = 3))  %>%
  column_spec(c(2,3,6), width="4em") %>% 
  column_spec(c(4,7), width="3.5em") %>% 
  column_spec(c(5,8), width="7em") %>%
  group_rows("Primary Index of Serious Violence", 2, 2) %>%
  group_rows("Primary Outcome Components, Counts", 3, 5)




table


writeLines(table, cl_args$OUT_FILE)
writeLines(table, gsub("up_to_date", timestamp, cl_args$OUT_FILE))


```

\elandscape
